package zipStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Demo2 {
    public static void main(String[] args) throws IOException {
        //压缩单个文件
        File src = new File("D:\\temp\\jack.txt");
        File dest = new File("D:\\temp\\");
        toZip(src,dest);
    }

    private static void toZip(File src, File dest) throws IOException {
        //1.创建压缩流关联压缩包
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest,"a.zip")));
        //2.创建ZipEntry对象
        ZipEntry entry = new ZipEntry("a.txt");
        //3.将压缩包添加到压缩包中
        zos.putNextEntry(entry);
        //4.将文件数据进行写入
        FileInputStream fis = new FileInputStream(src);
        int b;
        while((b = fis.read()) != -1){
            zos.write(b);
        }
        //释放资源
        fis.close();
        zos.closeEntry();
        zos.close();

    }
}
